package com.stillalive.blogsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.stillalive.blogsystem.common.constant.Constants;
import com.stillalive.blogsystem.common.exception.BlogException;
import com.stillalive.blogsystem.common.util.BeanTransUtils;
import com.stillalive.blogsystem.mapper.BlogInfoMapper;
import com.stillalive.blogsystem.pojo.dataobject.BlogInfo;
import com.stillalive.blogsystem.pojo.request.AddBlogRequest;
import com.stillalive.blogsystem.pojo.request.UpdateBlogRequest;
import com.stillalive.blogsystem.pojo.response.BlogInfoResponse;
import com.stillalive.blogsystem.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static javax.management.Query.eq;

@Slf4j
@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogInfoMapper blogInfoMapper;

    @Override
    public List<BlogInfoResponse> getList() {
        QueryWrapper<BlogInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogInfo::getDeleteFlag, Constants.UNDELETE_FLAG);
        List<BlogInfo> blogInfos = blogInfoMapper.selectList(queryWrapper);

        // BlogInfo转为BlogInfoResponse类型
        List<BlogInfoResponse> blogInfoResponses = blogInfos.stream()
                .map(blogInfo-> BeanTransUtils.trans(blogInfo))
                .collect(Collectors.toList());
        return  blogInfoResponses;
    }

    @Override
    public BlogInfoResponse getBlogDetail(Integer blogId) {
        BlogInfo blogInfo = getBlogInfo(blogId);
        return BeanTransUtils.trans(blogInfo);
    }

    public BlogInfo getBlogInfo(Integer blogId) {
        QueryWrapper<BlogInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogInfo::getDeleteFlag, Constants.UNDELETE_FLAG)
                .eq(BlogInfo::getId, blogId);
        return blogInfoMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean addBlog(AddBlogRequest addBlogRequest) {
        BlogInfo blogInfo = new BlogInfo();
        BeanUtils.copyProperties(addBlogRequest, blogInfo);
        try{
            Integer result = blogInfoMapper.insert(blogInfo);
            if(result == 1){
                return true;
            }
            return false;
        }catch (Exception e){
            log.error("博客插入失败，e:{}" + e);
            throw new BlogException("内部错误，请联系管理员..");
        }
    }

    @Override
    public Boolean updateBlog(UpdateBlogRequest updateBlogRequest) {
        BlogInfo blogInfo = BeanTransUtils.trans(updateBlogRequest);
        try {
            Integer result = blogInfoMapper.updateById(blogInfo);
            return result == 1;
        } catch (Exception e) {
            log.error("更新博客失败，e", e);
            throw new BlogException("内部错误，请联系管理员..");
        }
    }

    @Override
    public Boolean deleteBlog(Integer blogId) {
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(Constants.DELETE_FLAG);
        try {
            Integer result = blogInfoMapper.updateById(blogInfo);
            return result == 1;
        } catch (Exception e) {
            log.error("更新博客失败，e", e);
            throw new BlogException("内部错误，请联系管理员..");
        }
    }
}
